package opp.arrayadd;

public class Fib {
    public static void main(String[] args) {

        int num = 20;
        int sum=0;
        //求第i位的斐波那契数值
        System.out.printf("斐波那契数的第%d位是：", num);
        System.out.println(Fib(num));
        //求前i位的斐波那契数的按顺序输出
        System.out.printf("斐波那契数的前%d位分别是：", num);
        for (int i = 1; i <= num; i++) {
            System.out.print(Fib(i) + " ");
            sum+=Fib(i);
        }
        System.out.printf("\n斐波那契的前%d位的和是%d",num,sum);
    }
    public static int Fib(int n){
        if(n==1||n==2) {
            return 1;
        }
        return Fib(n -1)+Fib( n -2);
    }

}
